﻿using System.Collections.Generic;
using FikriKibar.Business.Abstract;
using FikriKibar.Common.Enums;
using FikriKibar.Common.Helpers;
using FikriKibar.Common.Mailing;
using FikriKibar.Common.Mailing.Entities;
using FikriKibar.Common.Mailing.Helpers;
using FikriKibar.DataAccess;
using FikriKibar.DataAccess.Abstract;
using FikriKibar.Entities.Concrete;
using FikriKibar.Globalization;

namespace FikriKibar.Business.Concrete
{
    public class EmailManager : IEmailService
    {
        #region Global Declarations
        private readonly ISettingDal _settingDal = new EfSettingDal();
        #endregion
        #region Methods
        public void Send(User user, string templateFilePath, EmailTypes emailTypes)
        {
            var templateText = FileHelper.ReadAllLines(templateFilePath);
            var eMailMessage = new EmailMessage();
            var from = new EmailAddress
            {
                DisplayName = _settingDal.GetValueByKey("SmtpSenderName"),
                Address = _settingDal.GetValueByKey("SmtpSenderMail")
            };

            eMailMessage.To = new List<EmailAddress>
            {
                new EmailAddress
                {
                    Address = user.Email
                }
            };
            var eMailSubject = "";
            var emailRow = new EmailRow();
            var emailKeys = new List<EmailKey>
            {
                new EmailKey
                {
                    Key = EmailKeyOptions.ApplicationName,
                    Value = _settingDal.GetValueByKey("ApplicationName")
                },
                new EmailKey
                {
                    Key = EmailKeyOptions.ApplicationUrl,
                    Value = _settingDal.GetValueByKey("ApplicationUrl")
                },
                new EmailKey
                {
                    Key = EmailKeyOptions.FirstName,
                    Value = user.FirstName
                },
                new EmailKey
                {
                    Key = EmailKeyOptions.LastName,
                    Value = user.LastName
                }
            };

            switch (emailTypes)
            {
                case EmailTypes.Add:
                    {
                        eMailSubject = Resources.UserInformation;
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Password,
                            Value = user.Password
                        });
                        break;
                    }

                case EmailTypes.Activation:
                    {
                        eMailSubject = Resources.UserInformation;
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Password,
                            Value = user.Password
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.ActivationCode,
                            Value = SecurityHelper.Encrypt(user.Id + "@" + user.CreateDate)
                        });
                        break;
                    }
                case EmailTypes.Update:
                    {
                        eMailSubject = Resources.UserInformation;
                        break;
                    }
                case EmailTypes.UpdatePassword:
                    {
                        eMailSubject = Resources.NewPassword;
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Password,
                            Value = user.Password
                        });
                        break;
                    }
                case EmailTypes.ForgotPassword:
                    {
                        eMailSubject = Resources.NewPassword;

                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = EmailKeyOptions.Password,
                            Value = user.Password
                        });
                        break;
                    }
            }

            emailKeys.Add(new EmailKey
            {
                Key = EmailKeyOptions.Subject,
                Value = eMailSubject
            });

            emailRow.EmailKeys = emailKeys;
            eMailMessage.Subject = eMailSubject;
            eMailMessage.From = from;
            eMailMessage.IsBodyHtml = true;
            eMailMessage.Priority = EmailPriority.Normal;
            eMailMessage.Body = EmailHelper.ConvertTemplateToString(templateText, emailRow);

            var smtp = new SystemNetSmtp(new FikriKibarSmtpClient
            {
                EnableSsl = _settingDal.GetValueByKey("SmtpSsl").ConvertToBoolean(),
                Host = _settingDal.GetValueByKey("SmtpServer"),
                Port = _settingDal.GetValueByKey("SmtpPort").ConvertToInt(),
                Username = _settingDal.GetValueByKey("SmtpUser"),
                Password = _settingDal.GetValueByKey("SmtpPassword")
            });

            smtp.Send(eMailMessage);
        }
        #endregion
    }
}
